#!/usr/bin/env pypy

import unittest, os, math, uuid

import playlist_module

from MPD import MPD

class TestMPD(unittest.TestCase):
	# Woefully inadequite. Need to seriously add a ton of tests to make this work.

	@classmethod
	def setUpClass(cls):
		cls.playlist_name = uuid.uuid4()

		cls.crossfade = MPD.crossfade
		cls.consume = MPD.consume
		cls.mixrampdb = MPD.mixrampdb
		cls.mixrampdelay = MPD.mixrampdelay
		cls.state = MPD.status["state"]
		cls.random = MPD.random
		cls.repeat = MPD.repeat
		cls.replay_gain = MPD.replay_gain
		cls.single = MPD.single

		MPD.pause = True

		if "song" in MPD.status:
			cls.curr_pos = (MPD.status["song"], MPD.time[0])

		MPD.save(cls.playlist_name)

		MPD.stop = True

		MPD.clear()


	@classmethod
	def tearDownClass(cls):
		MPD.stop = True

		MPD.clear()

		MPD.crossfade = cls.crossfade
		MPD.consume = cls.consume
		MPD.mixrampdb = cls.mixrampdb
		MPD.mixrampdelay = cls.mixrampdelay
		MPD.random = cls.random
		MPD.repeat = cls.repeat
		MPD.replay_gain = cls.replay_gain
		MPD.single = cls.single

		MPD.playlist = cls.playlist_name

		del MPD.playlists[cls.playlist_name]
	
		if cls.state == "play":
			MPD.play = MPD[cls.curr_pos[0]]

			MPD.seek(cls.curr_pos[1])
		elif cls.state == "pause":
			MPD.play = MPD[cls.curr_pos[0]]

			MPD.seek(cls.curr_pos[1])

			MPD.pause = True
		else:
			MPD.stop



	def test_find(self):
		find = MPD.all_files.find(MPD.FIND_TYPES.file, "Test", strict = False)

                MPD.all_files.find(MPD.FIND_TYPES.file, "Test", strict = False, add = True)

		self.assertEqual(MPD, find)

		self.assertEqual(find[0], MPD.all_files.find(MPD.FIND_TYPES.file, find[0])[0])


	def test_stickers(self):
		track = MPD.all_files.find(MPD.FIND_TYPES.file, "Test", strict = False)[0]

		old_stickers = {key: value for key, value in track.stickers.iteritems()}

		track.stickers.clear()

		self.assertEqual(len(track.stickers), 0)

		track.stickers["test"] = "I'm a test"

		self.assertEqual(len(track.stickers), 1)

		self.assertEqual(track.stickers["test"], "I'm a test")

		track.stickers["test2"] = "I'm another test"

		self.assertEqual(len(track.stickers), 2)

		self.assertTrue(track.stickers.has_key("test2"))

		self.assertFalse(track.stickers.has_key("test3"))

		self.assertEqual(track.stickers, dict(track.stickers.items()))

		del track.stickers["test2"]

		self.assertEqual(len(track.stickers), 1)

		track.stickers.clear()

		track.stickers.update(old_stickers)


	def test_update(self):
		MPD.update = True
		self.assertTrue(MPD.update)

if __name__ == '__main__':
	unittest.main()

